import Taro, { Component } from '@tarojs/taro'
import { View, Text, Image } from '@tarojs/components'
import { AtButton } from 'taro-ui'
import { DNavBar } from '@/components'
import http from '@/api'
import './index.scss'
import { handleIdCard } from '@/utils'

const conf = require('../../../config/conf')
const successIcon = require('../../assets/images/success@2x.png')
const timeIcon = require('../../assets/images/time@2x.png')

export default class PayAccount extends Component {
  state = {
    title: '企业收款账户',
    accountInfo: null, // 账户详情
  }

  componentDidMount() {
    this.infoByEnterprise()
  }

  async infoByEnterprise() {
    let accountInfo = await http.infoByEnterprise()

    this.setState({
      accountInfo,
      status: accountInfo.status
    })
  }
  handleStatusText(status) {
    let statusObj = {
      '0': '审核通过',
      '1': '审核中',
      '2': '审核不通过'
    }
    return statusObj[status]
  }
  async updataPassword(operType) {
    const { infoData, bodyData, signData } = await http.updataPassword(operType);
    this.setState({
      bodyData, infoData, signData
    }, () => {
      this.refs.submit.click()
    })
  }
  // 修改交易密码
  handleEditPwd() {
    this.updataPassword(17)
  }
  // 忘记交易密码
  handleforgetPwd() {
    this.updataPassword(18);
  }
  handleStatusBtn(status) {
    if (status === "1") {
      Taro.navigateBack();
    } else {
      Taro.navigateTo({
        url: "/pages/accountBook/accountBook?changeInfo=true"
      });
    }
  }

  render() {
    let {
      title,
      accountInfo,
      infoData,
      bodyData,
      signData,
      status
    } = this.state

    return (
      <View className="pay_account">
        <DNavBar title={title}></DNavBar>

        {
          status === "0" && <View className="container">
            <View className="content">
              <View className="status">
                <Text>状态</Text>
                <Text className="tag pass">{this.handleStatusText(accountInfo.status)}</Text>
              </View>

              <View className="body">
                <View className="body_item">
                  <Text className="left_text">账户户名</Text>
                  <Text className="right_text">{accountInfo.accountNm}</Text>
                </View>
                <View className="body_item">
                  <Text className="left_text">账号</Text>
                  <Text className="right_text">{accountInfo.account}</Text>
                </View>
                <View className="body_item">
                  <Text className="left_text">公司名称</Text>
                  <Text className="right_text">{accountInfo.busFullNm}</Text>
                </View>
                <View className="body_item">
                  <Text className="left_text">法人真实姓名</Text>
                  <Text className="right_text">{accountInfo.legalPerNm}</Text>
                </View>
                <View className="body_item">
                  <Text className="left_text">法人身份证号码</Text>
                  <Text className="right_text">{handleIdCard(accountInfo.agentIdNo)}</Text>
                </View>
                <View className="body_item">
                  <Text className="left_text">被授权人</Text>
                  <Text className="right_text">{accountInfo.agent}</Text>
                </View>
              </View>
            </View>

            <View className="btns">
              <AtButton className="button" onClick={() => {
                Taro.navigateTo({
                  url: '/pages/accountBook/accountBook?changeInfo=true'
                })
              }}>更改信息</AtButton>
              <AtButton className="button" onClick={this.handleEditPwd.bind(this)}>修改交易密码</AtButton>
              <AtButton className="button" onClick={this.handleforgetPwd.bind(this)}>忘记交易密码</AtButton>
            </View>

          </View>
        }
        {
          (status === "2" || status === "1") && <View className="container">
            <View className="status-tip">
              <Image src={status === "1" ? timeIcon : successIcon} className="status-icon"></Image>
              <View className="status-text">
                <Text>审核状态：</Text>
                <Text>{status === "1" ? "审核中" : "审核不通过"}</Text>
              </View>
            </View>
            <AtButton className="status-btn button" onClick={this.handleStatusBtn.bind(this, status)}>{status === "1" ? "返回" : "重新提交申请"}</AtButton>
            <View className="status-info">
              <View className="status-info-item">
                <View className="status-info-item-label">申请时间：</View>
                <View className="status-info-item-con">{accountInfo && accountInfo.createTime}</View>
              </View>
              <View className="status-info-item">
                <View className="status-info-item-label">审核结果：</View>
                <View className="status-info-item-con">{status === "1" ? "请耐心银行管理员审核" : "审核不通过"}</View>
              </View>
              {
                status === "2" && <View className="status-info-item">
                  <View className="status-info-item-label">备注：</View>
                  <View className="status-info-item-con">{accountInfo && accountInfo.remark || "无"}</View>
                </View>
              }
            </View>
          </View>
        }

        {/* 跳转龙存管页面 */}
        <form action={conf.fromURL} method="post">
          <input type="hidden" name="INFO" value={infoData} />
          <input type="hidden" name="CONTENTTYPE" value="json" />
          <input type="hidden" name="charset" value="GBK" />
          <input type="hidden" name="BODY" value={bodyData} />
          <input type="hidden" name="SIGN" value={signData} />
          <input type="submit" ref="submit" value="Submit" style={{ opacity: 0, width: 0, height: 0 }} />
        </form>
      </View>
    )
  }
}
